load("//rs/tests:common.bzl", "GRAFANA_RUNTIME_DEPS", "MESSAGE_CANISTER_ENV", "MESSAGE_CANISTER_RUNTIME_DEPS", "SIGNER_CANISTER_ENV", "SIGNER_CANISTER_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "system_test_nns")

package(default_visibility = ["//rs:system-tests-pkg"])

system_test_nns(
    name = "tecdsa_complaint_test",
    env = MESSAGE_CANISTER_ENV,
    uses_guestos_img = False,
    uses_guestos_malicious_img = True,
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "tecdsa_add_nodes_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/nns/governance/api",
        "//rs/registry/canister",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "tschnorr_message_sizes_test",
    env = MESSAGE_CANISTER_ENV | SIGNER_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SIGNER_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
    ],
)

system_test_nns(
    name = "tecdsa_remove_nodes_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:rand",
        "@crate_index//:rand_chacha",
        "@crate_index//:slog",
    ],
)

system_test_nns(
    name = "tecdsa_key_rotation_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "pre_signature_stash_management_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "tecdsa_signature_fails_without_cycles_test",
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 1.05% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "long_test",
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:slog",
    ],
)

system_test_nns(
    name = "tecdsa_signature_from_nns_without_cycles_test",
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 1.67% over the month from 2025-02-11 till 2025-03-11 only for the //rs/tests/consensus/tecdsa:tecdsa_signature_from_nns_without_cycles_test_head_nns variant.
    tags = [
        "long_test",
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "tecdsa_signature_from_other_subnet_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
        "@crate_index//:itertools",
    ],
)

system_test_nns(
    name = "tecdsa_two_signing_subnets_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/nns/constants",
        "//rs/registry/canister",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:slog",
    ],
)

system_test_nns(
    name = "tecdsa_signature_life_cycle_test",
    env = MESSAGE_CANISTER_ENV,
    # Remove when CON-937 is resolved
    flaky = True,  # flakiness rate of 1.57% over the month from 2025-02-11 till 2025-03-11
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    test_timeout = "eternal",
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/nns/constants",
        "//rs/registry/canister",
        "//rs/registry/nns_data_provider",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

system_test_nns(
    name = "tecdsa_signature_same_subnet_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "tecdsa_signature_timeout_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "long_test",  # since it takes longer than 5 minutes.
    ],
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/config",
        "//rs/nns/constants",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
    ],
)

tecdsa_performance_test_template = system_test_nns(
    name = "tecdsa_performance_test_template",
    colocated_test_driver_vm_required_host_features = ["performance"],
    colocated_test_driver_vm_resources = {
        "vcpus": 64,
        "memory_kibibytes": 512142680,
        "boot_image_minimal_size_gibibytes": 500,
    },
    crate_features = select({
        "//bazel:upload_perf_systest_results_enabled": ["upload_perf_systest_results"],
        "//conditions:default": [],
    }),
    tags = [
        "colocate",
        "manual",
    ],
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/sys",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:chrono",
        "@crate_index//:futures",
        "@crate_index//:reqwest",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:tokio",
    ],
)

DEFAULT_NODES_COUNT = str(25)

system_test_nns(
    name = "tecdsa_performance_test",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = [
        "performance",
        "spm",
    ],
    colocated_test_driver_vm_resources = {
        "vcpus": 64,
        "memory_kibibytes": 512142680,
        "boot_image_minimal_size_gibibytes": 500,
    },
    enable_head_nns_variant = False,
    env = MESSAGE_CANISTER_ENV | SIGNER_CANISTER_ENV | {
        "TECDSA_PERFORMANCE_TEST_KEY_IDS": "ecdsa_secp256k1",
        "BENCHMARK_NAME": "tecdsa_performance_test",
        "DEFAULT_NODES_COUNT": DEFAULT_NODES_COUNT,
    },
    tags = [
        "colocate",
        "manual",
    ],
    test_driver_target = tecdsa_performance_test_template.test_driver_target,
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + MESSAGE_CANISTER_RUNTIME_DEPS + SIGNER_CANISTER_RUNTIME_DEPS,
)

system_test_nns(
    name = "tschnorr_ed25519_performance_test",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = [
        "performance",
        "spm",
    ],
    colocated_test_driver_vm_resources = {
        "vcpus": 64,
        "memory_kibibytes": 512142680,
        "boot_image_minimal_size_gibibytes": 500,
    },
    enable_head_nns_variant = False,
    env = MESSAGE_CANISTER_ENV | SIGNER_CANISTER_ENV | {
        "TECDSA_PERFORMANCE_TEST_KEY_IDS": "schnorr_ed25519",
        "BENCHMARK_NAME": "tschnorr_ed25519_performance_test",
        "DEFAULT_NODES_COUNT": DEFAULT_NODES_COUNT,
    },
    tags = [
        "colocate",
        "manual",
    ],
    test_driver_target = tecdsa_performance_test_template.test_driver_target,
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + MESSAGE_CANISTER_RUNTIME_DEPS + SIGNER_CANISTER_RUNTIME_DEPS,
)

system_test_nns(
    name = "tschnorr_bip340_performance_test",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = [
        "performance",
        "spm",
    ],
    colocated_test_driver_vm_resources = {
        "vcpus": 64,
        "memory_kibibytes": 512142680,
        "boot_image_minimal_size_gibibytes": 500,
    },
    enable_head_nns_variant = False,
    env = MESSAGE_CANISTER_ENV | SIGNER_CANISTER_ENV | {
        "TECDSA_PERFORMANCE_TEST_KEY_IDS": "schnorr_bip340",
        "BENCHMARK_NAME": "tschnorr_bip340_performance_test",
        "DEFAULT_NODES_COUNT": DEFAULT_NODES_COUNT,
    },
    tags = [
        "colocate",
        "manual",
    ],
    test_driver_target = tecdsa_performance_test_template.test_driver_target,
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + MESSAGE_CANISTER_RUNTIME_DEPS + SIGNER_CANISTER_RUNTIME_DEPS,
)

system_test_nns(
    name = "vetkd_performance_test",
    additional_colocate_tags = [
        "system_test_benchmark",
    ],
    colocated_test_driver_vm_required_host_features = [
        "performance",
        "spm",
    ],
    colocated_test_driver_vm_resources = {
        "vcpus": 64,
        "memory_kibibytes": 512142680,
        "boot_image_minimal_size_gibibytes": 500,
    },
    enable_head_nns_variant = False,
    env = MESSAGE_CANISTER_ENV | SIGNER_CANISTER_ENV | {
        "TECDSA_PERFORMANCE_TEST_KEY_IDS": "vetkd_bls12_381_g2",
        "BENCHMARK_NAME": "vetkd_performance_test",
        "DEFAULT_NODES_COUNT": DEFAULT_NODES_COUNT,
    },
    tags = [
        "colocate",
        "manual",
    ],
    test_driver_target = tecdsa_performance_test_template.test_driver_target,
    test_timeout = "eternal",
    runtime_deps = GRAFANA_RUNTIME_DEPS + MESSAGE_CANISTER_RUNTIME_DEPS + SIGNER_CANISTER_RUNTIME_DEPS,
)
